import 'package:flutter/material.dart';

class HomeSectionTabMenu extends StatefulWidget {
  final int curIndex;
  final List<String> tabTitles;
  final TabController? tabController;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? labelPadding;
  final bool? gradient;

  const HomeSectionTabMenu(
    this.tabTitles, {
    super.key,
    this.curIndex = 0,
    this.tabController,
    this.padding,
    this.labelPadding,
    this.gradient = false,
  });

  @override
  State<StatefulWidget> createState() {
    return _HomeSectionTabMenuState();
  }
}

class _HomeSectionTabMenuState extends State<HomeSectionTabMenu> {
  late int curIndex = widget.curIndex;
  final itemKeys = [];

  @override
  void initState() {
    super.initState();
    // widget.tabController?.addListener(_listener);
  }

  void _listener() {
    print('_listener ${widget.tabController?.offset}');
    int tabIndex = widget.tabController?.index ?? 0;
    if (tabIndex != curIndex) {
      curIndex = tabIndex;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return TabBar(
      tabAlignment: TabAlignment.center,
      padding: EdgeInsets.zero,
      controller: widget.tabController,
      tabs: List.generate(
        widget.tabTitles.length,
        (index) => Padding(
          padding: widget.padding != null ? widget.padding! : const EdgeInsets.symmetric(horizontal: 12, vertical: 2),
          child: Text(widget.tabTitles[index], maxLines: 1),
        ),
      ),
      indicatorWeight: 0,
      isScrollable: true,
      labelPadding: widget.labelPadding != null ? widget.labelPadding! : EdgeInsets.symmetric(horizontal: 5),
      labelStyle: const TextStyle(
        fontSize: 13,
        color: Colors.white,
        fontWeight: FontWeight.w500,
        height: 18 / 12,
      ),
      unselectedLabelStyle: TextStyle(
        fontSize: 13,
        fontWeight: FontWeight.w400,
        color: Color(0xff999999),
        height: 18 / 12,
      ),
      indicator: BoxDecoration(
        color: widget.gradient == true ? null : Color(0xff0360FC),
        borderRadius: BorderRadius.circular(40),
        gradient: widget.gradient == true
            ? LinearGradient(
                colors: [Color(0xff84A4F9), Color(0xff2B5DDE)],
              )
            : null,
      ),
    );
  }

  @override
  void dispose() {
    widget.tabController?.removeListener(_listener);
    super.dispose();
  }
}
